<?php
namespace App\Admin\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Layout\Content;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
class ApimgrController extends Controller {
	/*
	 * GET			/api_mgr			index
	 * GET			/api_mgr/create		create
	 * POST			/api_mgr			store
	 * GET			/api_mgr/{id}		show
	 * GET			/api_mgr/{id}/edit	edit
	 * PUT/PATCH	/api_mgr/{id}		update
	 * DELETE		/api_mgr/{id} 		destroy
	*/
	public function __construct() {

	}

	public function index(Content $content) {
		return Admin::content(function (Content $content) {
			$tabs = DB::table("api_mgr")->where('pid','=',0)->get([
				'id','label','desc'
			]);
			$tabs = json_decode(json_encode($tabs),true);
			foreach($tabs as &$tab){
				$cfgs = DB::table("api_mgr")->where('pid','=',$tab['id'])->get();
				$cfgs = json_decode(json_encode($cfgs),true);
				$tab['cfg'] = $cfgs;
			}
            $content->header("API Manager");
            $content->description("API管理");
			$content->body(view("admin.api_mgr.index",[
				"Tabs" => $tabs,
			]));
        });
	}

	public function create(Content $content,Request $request) {
		$pid = $request['id'];
		return Admin::content(function (Content $content)use($pid) {
			$tabInfo = DB::table("api_mgr")->where("id",'=',$pid)->first();
			$tabInfo = json_decode(json_encode($tabInfo),true);
            $content->header("API Manager");
            $content->description($tabInfo['label']."的配置");
			$content->body(view("admin.api_mgr.create",[
				"Pid" => $pid
			]));
        });
	}

	public function store(Request $request) {
		$data = $request->all();
		unset($data['s']);
		unset($data['_token']);
		DB::beginTransaction();
		try {
			DB::table("api_mgr")->insert($data);
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>"添加失败:".$e->getMessage()];
		}
	}

	public function show() {
		$tabs = DB::table("api_mgr")->where('pid','=',0)->get([
			'id','label','desc'
		]);
		$tabs = json_decode(json_encode($tabs),true);
		$form = [
			'create' => [
				"label" => "",
				"desc" => "",
				"_token" => csrf_token(),
			]
		];
		foreach($tabs as &$tab){
			$cfgs = DB::table("api_mgr")->where('pid','=',$tab['id'])->get();
			$cfgs = json_decode(json_encode($cfgs),true);
			$tab['cfg'] = $cfgs;
			foreach($cfgs as $cfg){
				$form["cfg_".$cfg['id']] = [
				   "id" => $cfg['id'],
				   "label" => $cfg['label'],
				   "k" => $cfg['k'],
				   "v" => $cfg['v'],
				   "desc" => $cfg['desc'],
				   "_token" => csrf_token(),
			   ];
			}
		}
		return ['code'=>1,'msg'=>'ok','data'=>$form];
	}

	public function edit($id,Content $content) {
		return Admin::content(function (Content $content)use($id) {
			$tabInfo = DB::table("api_mgr")->where("id",'=',$id)->first();
			$tabInfo = json_decode(json_encode($tabInfo),true);
	        $content->header("API Manager");
	        $content->description("编辑".$tabInfo['label']);
			$content->body(view("admin.api_mgr.edit",[
				"TabInfo" => $tabInfo
			]));
		});
	}

	public function update(Request $request) {
		$data = $request->all();
		unset($data['_token']);
		unset($data['s']);
		DB::beginTransaction();
		try {
			DB::table("api_mgr")->where("id","=",$data['id'])->update($data);
			DB::commit();
			return ['code'=>1,'msg'=>'编辑成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>"编辑失败:".$e->getMessage()];
		}
	}

	public function destroy($id) {
		DB::beginTransaction();
		try {
			$sub_ids = DB::table("api_mgr")->where("pid","=",$id)->pluck('id');
			DB::table("api_mgr")->whereIn("id",$sub_ids)->delete();
			DB::table("api_mgr")->where("id","=",$id)->delete();
			DB::commit();
			return ['code'=>1,'msg'=>'删除成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>"删除失败:".$e->getMessage()];
		}
	}
}
